<?php

namespace App\Http\Models\Permissions;

use App\Common\Utils\Common;
use App\Http\Models\Log\LogService;
use Illuminate\Support\Facades\Cache;
use App\Http\Service\menus\MenusUserService;
use App\Http\Models\Permissions\Permissions;

/*
 * PermissionsService  权限操作服务只处理逻辑
 *
 * index() 权限列表
 * show() 权限查看
 *
 * @author 金飞虎
 * @email 870741306@qq.com
 * @date 2018/4/5
 * */

class PermissionsService extends Permissions
{

    public function index($request)
    {
        return Permissions::select()
            ->where('is_menus', 0)
            ->where('permissions_name', 'like', '%' . $request->permissions_name . '%')
            ->orderBy('created_at', 'desc')
            ->paginate(PAGES);
    }

    public function getAllMenusHtml()
    {
        $menusDatas = (new MenusUserService())->getAllMenus();
        $data = [];
        $ids = [];
        $string = '|----';
        foreach ($menusDatas as $key => $menusData) {
            $data[]['permissions_name'] = $string . $menusData['permissions_name'];
            $ids[]['id'] = $menusData['id'];
            foreach ($menusData['son'] as $menus) {
                $data[]['permissions_name'] = $string . $string . $menus['permissions_name'];
                $ids[]['id'] = $menus['id'];
                foreach ($menus['son'] as $k => $val) {
                    $data[]['permissions_name'] = $string . $string . $string . $val['permissions_name'];
                    $ids[]['id'] = $val['id'];
                }
            }
        }
        return [
            'ids' => $ids,
            'menusData' => $data
        ];
    }

    public function store($request)
    {
        $ids = explode(',', $request->menusId);
        $data = [
            'pid' => end($ids),
            'permissions_name' => $request->permissions_name,
            'urls' => $request->urls,
            'icon' => $request->icons
        ];
        try {
            $res = Permissions::create($data);
            if ($res->id) {
                Common::success(INSERT_SUCCES);
                //操作日志
                LogService::log()->create(INSERT_PERMISSION . "-" . $request->permissions_name);
            } else {
                Common::error(INSERT_ERROR);
            }
        } catch (Exception $ex) {
            Common::error(INSERT_ERROR);
        }
    }

    public function updateSave($request, $id)
    {
        $data = [
            'permissions_name' => $request->permissions_name,
            'urls' => $request->urls,
            'icon' => $request->icons
        ];
        $permissionsDatas = [];
        if ($request->ids) {
            foreach ($request->ids as $key => $val) {
                $permissionsDatas[]['id'] = $val;
                $permissionsDatas[$key]['menus_name'] = $request->menus_name[$key];
                $permissionsDatas[$key]['permissions_url'] = $request->permissions_url[$key];
                $permissionsDatas[$key]['permissions_method'] = $request->permissions_method[$key];
            }
        }
        try {
            $res = Permissions::where('id', decryptUrl($id))->update($data);

            if ($res) {
                foreach ($permissionsDatas as $key => $val) {
                    Permissions::where('id', $val['id'])->update([
                        'permissions_name' => $val['menus_name'],
                        'urls' => $val['permissions_url'],
                        'method' => strtoupper($val['permissions_method']),
                    ]);
                }
                Cache::flush();
                Common::success(UPDATE_SUCCES);
                //操作日志
                LogService::log()->create(UPDATE_PERMISSION . "-" . $request->permissions_name);
            } else {
                Common::error(UPDATE_ERROR);
            }
        } catch (Exception $ex) {
            Common::error(UPDATE_ERROR);
        }
    }

    public function show($id)
    {
        return Permissions::where('id', decryptUrl($id))->first();
    }

    public function menusView($id)
    {
        $permissionsDatas = Permissions::where([
            'pid' => decryptUrl($id),
            'is_menus' => 1
        ])
            ->orWhere('id', decryptUrl($id))
            ->orderBy('id', 'desc')
            ->get()
            ->toArray();
        array_walk($permissionsDatas, function (&$val) use ($id) {
            if ($val['id'] == decryptUrl($id)) {
                $val['pid'] = 0;
            }
        });
        return generateTree($permissionsDatas) ? generateTree($permissionsDatas) : $permissionsDatas;
    }

    public function permissionsCreate($request)
    {
        $data = [
            'pid' => $request->permissionsId,
            'permissions_name' => $request->permissions_name,
            'urls' => $request->urls,
            'is_menus' => 1,
            'method' => $request->menth
        ];
        $res = Permissions::create($data);
        if ($res->id) {
            Common::success(INSERT_SUCCES);
        } else {
            Common::error(INSERT_ERROR);
        }
    }

    public function remove($id)
    {
        return Permissions::where('id', decryptUrl($id))->delete();
    }
}
